package com.qiaofang.housewarehouse.facade.impl;

import com.qiaofang.common.util.DateUtil;
import com.qiaofang.common.util.StringUtil;
import com.qiaofang.housewarehouse.dao.domain.HswhSaleHouse;
import com.qiaofang.housewarehouse.dao.utils.ConfigUtils;
import com.qiaofang.housewarehouse.service.SaleHouseQueryInfoService;
import com.qiaofang.housewarehouse.service.SaleHouseQueryService;
import com.qiaofang.housewarehouse.stub.constants.HouseTypeEnum;
import com.qiaofang.housewarehouse.stub.constants.ResponseCode;
import com.qiaofang.housewarehouse.stub.constants.TableConstants;
import com.qiaofang.housewarehouse.stub.facade.SaleHouseQueryInfoFacade;
import com.qiaofang.housewarehouse.stub.request.HouseQueryDetailRequest;
import com.qiaofang.housewarehouse.stub.request.ListHouseQueryDetailByIdsRequest;
import com.qiaofang.housewarehouse.stub.request.SaleHouseQueryPriceRequest;
import com.qiaofang.housewarehouse.stub.response.ListSaleHouseQueryDetailByIdsResponse;
import com.qiaofang.housewarehouse.stub.response.SaleHouseQueryDetailListResponse;
import com.qiaofang.housewarehouse.stub.response.SaleHouseQueryDetailResponse;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@RestController
public class SaleHouseQueryInfoFacadeImpl implements SaleHouseQueryInfoFacade {
    private static final Logger logger = LoggerFactory.getLogger(SaleHouseQueryInfoFacadeImpl.class);
    @Resource
    private SaleHouseQueryInfoService saleHouseQueryInfoService;

    @Resource
    private SaleHouseQueryService saleHouseQueryService;

    @Override
    public SaleHouseQueryDetailListResponse getSaleHouseInfo(@RequestBody @Valid SaleHouseQueryPriceRequest saleHouseQueryRequest) {
        logger.info("查询售房源开始");
        SaleHouseQueryDetailListResponse response = new SaleHouseQueryDetailListResponse();
        try {
            Map<String, String> paramMap = new HashMap<>();
            if (StringUtils.isNotBlank(saleHouseQueryRequest.getPageIndex())) {
                paramMap.put("pageIndex", saleHouseQueryRequest.getPageIndex());
            }
            if (StringUtils.isNotBlank(saleHouseQueryRequest.getPageSize())) {
                paramMap.put("pageSize", saleHouseQueryRequest.getPageSize());
            }
            if (org.apache.commons.lang.StringUtils.isNotBlank(saleHouseQueryRequest.getContactMobiles())) {
                paramMap.put("contactMobiles", saleHouseQueryRequest.getContactMobiles());
            }
            if (org.apache.commons.lang.StringUtils.isNotBlank(saleHouseQueryRequest.getEstates())) {
                paramMap.put("estates", saleHouseQueryRequest.getEstates());
            }
            if (StringUtils.isNotBlank(saleHouseQueryRequest.getKeywords())) {
                paramMap.put("keywords", saleHouseQueryRequest.getKeywords());
            }
            if (StringUtils.isNotBlank(saleHouseQueryRequest.getCity())) {
                paramMap.put("city", saleHouseQueryRequest.getCity());
            }
            if (StringUtils.isNotBlank(saleHouseQueryRequest.getRegion())) {
                paramMap.put("region", saleHouseQueryRequest.getRegion());
            }
            if (StringUtils.isNotBlank(saleHouseQueryRequest.getDistrict())) {
                paramMap.put("district", saleHouseQueryRequest.getDistrict());
            }
            if (StringUtils.isNotBlank(saleHouseQueryRequest.getDecoration())) {
                paramMap.put("decoration", saleHouseQueryRequest.getDecoration());
            }
            if(saleHouseQueryRequest.getCollectionTime()!=null){
                paramMap.put("collectionTime", DateUtil.getDate(saleHouseQueryRequest.getCollectionTime()));
            }
            if(StringUtils.isNotBlank(saleHouseQueryRequest.getSources())){
                paramMap.put("sources", saleHouseQueryRequest.getSources());
            }
            if(StringUtils.isNotBlank(saleHouseQueryRequest.getAddress())){
                paramMap.put("address", saleHouseQueryRequest.getAddress());
            }
            if(StringUtils.isNotBlank(saleHouseQueryRequest.getHousingType())){
                paramMap.put("housingType", saleHouseQueryRequest.getHousingType());
            }
            if (org.apache.commons.lang.StringUtils.isNotBlank(saleHouseQueryRequest.getHouseType())) {
                String houseType = saleHouseQueryRequest.getHouseType();
                Map<String, Integer> stringIntegerMap = HouseTypeEnum.toMap();
                for (String key : stringIntegerMap.keySet()) {
                    if (houseType.contains(key)) {
                        paramMap.put("houseType", String.valueOf(stringIntegerMap.get(key)));
                    }
                }
            }
            if (StringUtils.isNotBlank(saleHouseQueryRequest.getStartSellPrice()) || StringUtils.isNotBlank(saleHouseQueryRequest.getEndSellPrice())) {
                paramMap.put("sellPriceRange", (StringUtils.isNotBlank(saleHouseQueryRequest.getStartSellPrice()) ? saleHouseQueryRequest.getStartSellPrice() : 0)
                        + "~" + (StringUtils.isNotBlank(saleHouseQueryRequest.getEndSellPrice()) ? saleHouseQueryRequest.getEndSellPrice() : 0));
            }
            if (StringUtils.isNotBlank(saleHouseQueryRequest.getStartRoomArea()) || StringUtils.isNotBlank(saleHouseQueryRequest.getEndRoomArea())) {
                paramMap.put("roomAreaRange", (StringUtils.isNotBlank(saleHouseQueryRequest.getStartRoomArea()) ? saleHouseQueryRequest.getStartRoomArea() : 0)
                        + "~" + (StringUtils.isNotBlank(saleHouseQueryRequest.getEndRoomArea()) ? saleHouseQueryRequest.getEndRoomArea() : 0));
            }
            if (StringUtils.isNotBlank(saleHouseQueryRequest.getStartYear()) || StringUtils.isNotBlank(saleHouseQueryRequest.getEndYear())) {
                paramMap.put("yearRange", (StringUtils.isNotBlank(saleHouseQueryRequest.getStartYear()) ? saleHouseQueryRequest.getStartYear() : 0)
                        + "~" + (StringUtils.isNotBlank(saleHouseQueryRequest.getEndYear()) ? saleHouseQueryRequest.getEndYear() : 0));
            }
            if (org.apache.commons.lang.StringUtils.isNotBlank(saleHouseQueryRequest.getStartFloor()) || org.apache.commons.lang.StringUtils.isNotBlank(saleHouseQueryRequest.getEndFloor())) {
                paramMap.put("floorRange", (org.apache.commons.lang.StringUtils.isNotBlank(saleHouseQueryRequest.getStartFloor()) ? saleHouseQueryRequest.getStartFloor() : 0)
                        + "~" + (org.apache.commons.lang.StringUtils.isNotBlank(saleHouseQueryRequest.getEndFloor()) ? saleHouseQueryRequest.getEndFloor() : 0));
            }

            logger.info("接受参数值：\n");
            for (String ks : paramMap.keySet()) {
                logger.info("key=" + ks + "," + "value=" + paramMap.get(ks) + "\t");
            }
            if(saleHouseQueryRequest.getAttentionFlag() == 1){
                response.setData(saleHouseQueryInfoService.searchSaleHouseList(paramMap,saleHouseQueryRequest.getHousingAcqAttentionDTOS()));
            }else{
                response.setData(saleHouseQueryInfoService.searchSaleHouseList(paramMap));
            }
            response.setResponseCode(ResponseCode.RC_SUCCESS.getResponseCode());
            response.setResponseMessage(ResponseCode.RC_SUCCESS.getResponseMessage());
            logger.info("查询售房源结束====SUCCESS");
        } catch (Throwable e) {
            response.setResponseCode(ResponseCode.RC_EXCEPTION_ERROR.getResponseCode());
            response.setResponseMessage(ResponseCode.RC_EXCEPTION_ERROR.getResponseMessage());
            logger.error("SaleHouseQueryInfoFacadeImpl error！", e);
        }

        return response;

    }

    @Override
    public SaleHouseQueryDetailResponse getSaleHouseDetail(@RequestBody @Valid HouseQueryDetailRequest saleHouseQueryRequest) {
        SaleHouseQueryDetailResponse response = new SaleHouseQueryDetailResponse();
        try {
            logger.info("查询售房源明细开始");
            Map<String, String> paramMap = new HashMap<>();
            paramMap.put("indexName", ConfigUtils.getIndexHswhSaleHouse());
            paramMap.put("typeName", TableConstants.INDEX_TYPE_SALE_HOUSE);
            response.setData(saleHouseQueryService.getSaleHouseDetail(paramMap,TableConstants.HSWH_SALE_HOUSE, saleHouseQueryRequest.getId(), HswhSaleHouse.class));
            response.setResponseCode(ResponseCode.RC_SUCCESS.getResponseCode());
            response.setResponseMessage(ResponseCode.RC_SUCCESS.getResponseMessage());
            logger.info("查询售房源明细结束=======SUCCESS");
        } catch (Exception ex) {
            response.setResponseCode(ResponseCode.RC_EXCEPTION_ERROR.getResponseCode());
            response.setResponseMessage(ResponseCode.RC_EXCEPTION_ERROR.getResponseMessage());
            logger.error("SaleHouseQueryFacadeImpl error！", ex);
        }
        return response;
    }

    @Override
    public ListSaleHouseQueryDetailByIdsResponse listSaleHouseDetailByIds(ListHouseQueryDetailByIdsRequest saleHouseQueryRequest) {
        ListSaleHouseQueryDetailByIdsResponse response = new ListSaleHouseQueryDetailByIdsResponse();
        try {
            logger.info("查询售房源明细开始");
            Map<String, String> paramMap = new HashMap<>();
            paramMap.put("indexName", ConfigUtils.getIndexHswhSaleHouse());
            paramMap.put("typeName", TableConstants.INDEX_TYPE_SALE_HOUSE);
            response.setData(saleHouseQueryInfoService.listSaleHouseDetailByIds(paramMap,TableConstants.HSWH_SALE_HOUSE,
                saleHouseQueryRequest.getIds(), HswhSaleHouse.class));
            response.setResponseCode(ResponseCode.RC_SUCCESS.getResponseCode());
            response.setResponseMessage(ResponseCode.RC_SUCCESS.getResponseMessage());
            logger.info("查询售房源明细结束=======SUCCESS");
        } catch (Exception ex) {
            response.setResponseCode(ResponseCode.RC_EXCEPTION_ERROR.getResponseCode());
            response.setResponseMessage(ResponseCode.RC_EXCEPTION_ERROR.getResponseMessage());
            logger.error("SaleHouseQueryFacadeImpl error！", ex);
        }
        return response;
    }
}
